from django.db import models
import os
import uuid

#python manage.py schemamigration store --auto
#python manage.py migrate store


class TinyShop(models.Model):
    def tinyshop_logo_filename(instance, filename):  # @NoSelf
        unused_f, ext = os.path.splitext(filename)
        return 'store/tinyshop/%s/%s%s' % (instance.mobile_no[-3:], uuid.uuid4().hex, ext)
    name = models.CharField(max_length=100)
    account = models.DecimalField(max_digits=10,decimal_places=2, default=0.0)
    sales = models.CharField(max_length=100)
    #logo = models.ImageField(upload_to=tinyshop_logo_filename,default='/default/store/tinyshop_no_img.jpg')
    balance = models.DecimalField(max_digits=10,decimal_places=2, default=0.0)
    mobile_no = models.CharField(unique=True, max_length=20)
    image = models.ForeignKey("TinyshopImage", default=None, blank=True, null=True)
    
    support_express = models.BooleanField(default=False)
    province = models.CharField(max_length=20, blank=True, null=True)
    address = models.CharField(max_length=255, blank=True, null=True)
    
    def __unicode__(self):
        return u"[%s] %s" % (self.mobile_no, self.name)
    

class ProductImage(models.Model):
    def product_image_filename(instance, filename):  # @NoSelf
        unused_f, ext = os.path.splitext(filename)
        return 'store/product/%s/%s/%s%s' % (instance.tinyshop.mobile_no[-6:-3], instance.tinyshop.mobile_no[-3:], uuid.uuid4().hex, ext)

    tinyshop = models.ForeignKey(TinyShop)
    image = models.ImageField(upload_to=product_image_filename,default='/default/store/product_no_img.jpg')
    
    def __unicode__(self):
        return u"[%s] %d" % (self.tinyshop, self.pk)

class TinyshopImage(models.Model):
    def tinyshop_image_filename(instance, filename):  # @NoSelf
        unused_f, ext = os.path.splitext(filename)
        return 'store/tinyshop/%s/%s/%s%s' % ("2014", "04", uuid.uuid4().hex, ext)

    image = models.ImageField(upload_to=tinyshop_image_filename,default='/default/store/product_no_img.jpg')
    
    def __unicode__(self):
        return u"%d" % self.pk

class Product(models.Model):
    Published = 1
    Trash = 10
    
    STATUS_TYPE_CHOICES = (
        (Published, 'Published'),
        (Trash, 'Trash'),
    )

    tinyshop = models.ForeignKey(TinyShop)
    name = models.CharField(max_length=100)
    image = models.ForeignKey(ProductImage, blank=True, null=True)
    price = models.DecimalField(max_digits=10,decimal_places=2)
    information = models.TextField()
    status = models.SmallIntegerField(choices=STATUS_TYPE_CHOICES,default=Published)
    stock = models.IntegerField(default=0)
    
    def __unicode__(self):
        return "[%s] %s" % (self.tinyshop.name, self.name)
    
# class User(models.Model):
#     call_phone = models.CharField(max_length=20)
#     balance = models.DecimalField(max_digits=10,decimal_places=2)

class Order(models.Model):
    Paid = 1
    New = 2
    Processing = 3
    Fraud = 4
    Completed = 5
    Closed = 6
    REFUND = 7
    
    PAYMENT_CASH = "Cash"
    PAYMENT_BANK_CARD = "BankCard"
    PAYMENT_PREPAID_CARD = 'PrepaidCard'
    
    STATUS_TYPE_CHOICES = (
        (Paid, 'Paid'),
        (New, 'New'),
        (Processing, 'Processing'),
        (Fraud, 'Fraud'),
        (Completed, 'Completed'),
    )
    
    PAYMENT_TYPE_CHOICES = (
        (PAYMENT_CASH, 'Cash'),
        (PAYMENT_BANK_CARD, 'Bank Card'),
        (PAYMENT_PREPAID_CARD, 'Prepaid Card'),
                            )
    
    tinyshop = models.ForeignKey(TinyShop)
    created_at = models.DateTimeField(auto_now_add=True)
    amount = models.DecimalField(max_digits=10,decimal_places=2)
    product_amount = models.DecimalField(max_digits=10,decimal_places=2)
    payer = models.CharField(db_index=True,max_length=20)
    detail = models.TextField()
    payment = models.CharField(max_length=100,choices=PAYMENT_TYPE_CHOICES,default=PAYMENT_BANK_CARD)
    memo = models.CharField(max_length=255, null=True)
    
    needexpress = models.BooleanField(default=False)
    address = models.CharField(max_length=200,default="",null=True)
    
    # zero expression not paying,one expression paid
    status = models.SmallIntegerField(choices=STATUS_TYPE_CHOICES,default=New)
    trans_id = models.CharField(max_length=100, default="", blank=True)
    
    def __unicode__(self):
        return u"[%s]Order: %s" % (self.tinyshop, self.pk)

class GoodsCategory(models.Model):
    name = models.CharField(max_length=100)
    
    def __unicode__(self):
        return self.name

class GoodsLib(models.Model):
    name = models.CharField(max_length=100)
    category = models.ForeignKey(GoodsCategory)
    price = models.DecimalField(max_digits=10,decimal_places=2, default=0.0)
    information = models.TextField()
    
    def __unicode__(self):
        return self.name
    
